package com.pearlsoft.webapp.core.web.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.tree.DefaultAttribute;

public class RightUtil {
	
	
	
	/**
	 * 创建用户权限菜单
	 * @param list
	 * @param userCode
	 */
	public static void buildUserRightXml(List<Map<String, Object>> list,String userCode){
		Document document = DocumentHelper.createDocument();
		Element root = document.addElement("MENUS");
		root.addAttribute("owner", userCode);
		for (Iterator<Map<String, Object>> iter = list.iterator(); iter.hasNext();) {
			Map<String, Object> _map = iter.next();
			Element menu = root.addElement("MENU");
			Iterator<?> _iterMap = _map.entrySet().iterator();
			while(_iterMap.hasNext()){
				 Map.Entry<String,?> entry = (Map.Entry) _iterMap.next();
				 Object key = entry.getKey();
				 String  val = entry.getValue()==null?"":entry.getValue().toString();
				 menu.addAttribute(key.toString(),val);
			}
		}
		try {
			String rightDir = System.getProperty("SYS_RIGHT_DIR");
			String path  = System.getProperty("APP_PATH")+rightDir;
		    File file = new File(path);
			if(!file.exists()){
				file.mkdirs();
			}
			path =path+userCode+".xml";
			OutputFormat format = OutputFormat.createPrettyPrint();  
			format.setEncoding("UTF-8"); 
			XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(path),format);
			xmlWriter.write(document);
			xmlWriter.flush();
			xmlWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}  
		
	}
	
	/**
	 * 根据属性名、属性值查找
	 * @param userCode
	 * @param arr
	 * @param value
	 * @return
	 */
	public static List<Element> queryElement(String userCode,String arr,String value){
		Document doc   = getUserXml(userCode);
		String xpath = "/MENUS/MENU[@"+arr+"='"+value+"']";
		List<Element> list = doc.selectNodes(xpath);
		return list;
	}
	
	/**
	 * 根据url 获取节点
	 * @param userCode
	 * @param url
	 * @return
	 */
	public  static Element getElementByUrl(String userCode,String url) {
			Document doc = getUserXml(userCode);
			String xpath = "/MENUS/MENU[@MENU_URL='"+url+"']";
			Element element =  (Element) doc.selectSingleNode(xpath);
			if(element!=null){
				return element;
			}
		return null;
	}
	
	/**
	 * 判断用户有没有url的访问权限
	 * @param userCode
	 * @param url
	 * @return
	 */
	public static boolean isRight(String userCode,String url){
		Element element = getElementByUrl(userCode, url);
		if(element!=null){
			return true;
		}
		return false;
	}
	
	public static Document getUserXml(String userCode){
		String rightDir = System.getProperty("SYS_RIGHT_DIR");
		String xmlFile = System.getProperty("APP_PATH")+rightDir+userCode+".xml";
		SAXReader reader = new SAXReader();
		try {
			Document doc = reader.read(new File(xmlFile));
			return doc;
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 根据menuCode 获取下级菜单
	 * @param userCode
	 * @param menuCode
	 * @return
	 */
	public static List<Map<String,Object>> getChildrenRight(String userCode,String menuCode){
		Document doc = getUserXml(userCode);
		String xpath = "/MENUS/MENU[@MENU_CODE='"+menuCode+"']";
		Element element =  (Element) doc.selectSingleNode(xpath);
		if(element!=null){
			String menuId=  element.attributeValue("MENU_ID");
			List<Map<String,Object>> rlist = new ArrayList<Map<String,Object>>();
			List<Element>  list = queryElement(userCode, "MENU_PARENT_ID", menuId);
			for(Iterator<Element> iterator = list.iterator();iterator.hasNext();){
				Element node = iterator.next();
				Iterator<DefaultAttribute> iterator2 =node.attributeIterator();
				Map<String,Object> _map = new HashMap<String, Object>();
				while (iterator2.hasNext()) {
					DefaultAttribute attribute =iterator2.next();
					_map.put(attribute.getName(), attribute.getValue());
				}
				rlist.add(_map);
			}
			return rlist;
		}
		
		return null;
	}
	

	
}
